﻿//-----------------------------------------------------------------------
// <copyright file="Settings.xaml.cs" company="Microsoft Corporation">
//     Copyright Microsoft Corporation. All Rights Reserved. This code released under the terms of the Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.) This is sample code only, do not use in production environments.
// </copyright>
//-----------------------------------------------------------------------

namespace vsarFlightPlan
{
    using System;
    using System.Linq;
    using Windows.Phone.UI.Input;
    using Windows.System;
    using Windows.UI.Popups;
    using Windows.UI.Xaml.Controls;

    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Settings : Page
    {
        /// <summary>
        /// The _view model
        /// </summary>
        private ViewModels.Settings _viewModel;

        public Settings()
        {
            this.InitializeComponent();
            HardwareButtons.BackPressed += HardwareButtons_BackPressed;
            this._viewModel = new ViewModels.Settings();
            this.DataContext = _viewModel;
        }

        private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
        {
            e.Handled = true;
            var valid = _viewModel.UpdateSettings();
            if (valid)
            {
                this.Frame.Navigate(typeof(MainPage));
            }
            else
            {
                await new MessageDialog("You haven't entered all the required information").ShowAsync();
            }
        }

        protected override void OnNavigatedFrom(Windows.UI.Xaml.Navigation.NavigationEventArgs e)
        {
            this.Frame.BackStack.Remove(this.Frame.BackStack.Last());
        }

        private async void LaunchSignupClick(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            await Launcher.LaunchUriAsync(new Uri("http://www.visualstudio.com/"));
        }
    }
}